﻿using System;
using System.Configuration;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using __ts.Core.Configuration;

namespace __ts.Core.Log
{
    /// <summary>
    /// 
    /// </summary>
    public class Logger
    {
        private static string logPath = string.Empty;
        /// <summary>
        /// 保存日志的文件夹
        /// </summary>
        public static string LogPath
        {
            get
            {
                if (string.IsNullOrEmpty(logPath))
                {
                    logPath = ConfigManager.GlobalAppSettings["logPath"];

                    if (string.IsNullOrEmpty(logPath))
                        logPath = ConfigurationManager.AppSettings["logPath"];

                    if (string.IsNullOrEmpty(logPath))
                    {
                        if (System.Web.HttpContext.Current == null)
                            logPath = AppDomain.CurrentDomain.BaseDirectory;
                        else
                            logPath = AppDomain.CurrentDomain.BaseDirectory + @"bin\";
                    }
                }
                return logPath;
            }
            set { logPath = value; }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static void Write(string info)
        {
            try
            {
                if (!Directory.Exists(LogPath))
                {
                    Directory.CreateDirectory(LogPath);
                }
                string file = string.Format("{0}\\{1:yyyyMMdd}.log", LogPath, DateTime.Now);
                info = string.Format("[{0}] {1}", DateTime.Now.ToString(), info);

                FileStream fs = new FileStream(file, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter streamWriter = new StreamWriter(fs);
                streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                streamWriter.WriteLine(info);
                streamWriter.Flush();
                streamWriter.Close();
                fs.Close();
            }
            catch { }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        public static void Write(Exception info)
        {
            Write(info.Message);
        }

        private static string GetConnectIP()
        {
            OperationContext context = OperationContext.Current;
            MessageProperties messageProperties = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            return endpointProperty.Address;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        /// <param name="channel"></param>
        public static void Write(Exception info, IClientChannel channel)
        {
            Write(new StringBuilder()
                .Append("\r\n===============================")
                .Append("\r\nService Name\t: ")
                .Append(channel.LocalAddress.Uri.Segments[1])
                .Append("\r\nIPAddress\t: ")
                .Append(GetConnectIP())
                .Append("\r\nMessage\t\t: ")
                .Append(info.Message)
                .Append("\r\n===============================")
                .ToString()
                );
        }

    }
}
